package pl.polsl.mmm.projectcomm.util;

import android.content.Context;
import android.content.res.Resources;
import pl.polsl.mmm.projectcomm.objects.OBJ;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class OBJParser {
	public static OBJ readOBJFileFromResource( Context context,
			int resourceId) {
        OBJ OBJ;
        int vertices, faces, texels, normals;

        vertices = faces = texels = normals = 0;
		try{
			InputStream inputStream = context.getResources().openRawResource(resourceId);
			InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
			BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
			
			String nextLine;
			while((nextLine = bufferedReader.readLine()) != null) {
                if (nextLine.startsWith("vt")) {
                    texels ++ ;
                } else if (nextLine.startsWith("vn")) {
                    normals ++;
                } else if (nextLine.startsWith("v")) {
                    vertices ++;
                } else if (nextLine.startsWith("f")) {
                    faces ++;
                }
			}
            OBJ = new OBJ(texels, vertices, faces, normals);
            inputStream = context.getResources().openRawResource(resourceId);
            inputStreamReader = new InputStreamReader(inputStream);
            bufferedReader = new BufferedReader(inputStreamReader);

            vertices = faces = texels = normals = 0;
            while((nextLine = bufferedReader.readLine()) != null) {
                if (nextLine.startsWith("vt")) {
                    float u,v;
                    String[] temp = nextLine.split(" ");
                    u = Float.parseFloat(temp[1]);
                    v = Float.parseFloat(temp[2]);
                    OBJ.putTexelData(texels, u, v);
                    texels ++;
                } else if (nextLine.startsWith("vn")) {
                    float x,y,z;
                    String[] temp = nextLine.split(" ");
                    x = Float.parseFloat(temp[1]);
                    y = Float.parseFloat(temp[2]);
                    z = Float.parseFloat(temp[3]);
                    OBJ.putNormalData(normals,x,y,z);
                    normals ++;
                } else if (nextLine.startsWith("v")) {
                    float x, y, z;
                    String[] temp = nextLine.split(" ");
                    x = Float.parseFloat(temp[1]);
                    y = Float.parseFloat(temp[2]);
                    z = Float.parseFloat(temp[3]);
                    OBJ.putVertexData(vertices, x, y, z);
                    vertices++;
                } else if (nextLine.startsWith("f")) {
                    int[] facesData = new int[9];
                    String[] temp = nextLine.split("[/ ]");
                    for(int i=0; i<9; i++) {
                        facesData[i] = Integer.parseInt(temp[i+1]);
                    }
                    OBJ.putFacesData(faces,facesData);
                    faces ++;
                }
            }

		} catch (IOException e) {
			throw new RuntimeException("Could not open resource: " + resourceId, e);
		} catch ( Resources.NotFoundException nfe) {
			throw new RuntimeException(" Resource not found: " + resourceId, nfe);
		}
        OBJ.fillVertexData();
		return OBJ;
	}
}
